//
//  GSectionView.m
//  helpinrain
//
//  Created by Gang.Wang on 12-8-7.
//  Copyright (c) 2012年 Zhuhai Yuike Technology Co., Ltd. All rights reserved.
//

#import "GSectionView.h"
#import "mcf.h"
#import "GSubSection.h"
#import "GSubSectionView.h"

@implementation GSectionView

@synthesize section = section_;
@synthesize subSectionViews = subSectionViews_;

- (void) dealloc
{
    YRelease(section_)
    YRelease(subSectionViews_)
    
    [super dealloc];
}


- (void) privateInit
{
    if (self.bInited)
    {
        return;
    }
    
    self.subSectionViews = [NSMutableArray array];
    
    [self reAdjustLayout];
    self.bInited = TRUE;
}

- (id) init
{
    self = [super init];
    if (self)
    {
        [self privateInit];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [self privateInit];
    }
    return self;
}

- (void) showDataToView
{
    for (GSubSection * subSecion in self.section.subSection)
    {
        GSubSectionView * subSectionView = [[GSubSectionView alloc] init];
        [subSectionView setSubSection:subSecion];
        [self addSubview:subSectionView];
        
        [self.subSectionViews addObject:subSectionView];
        
        YRelease(subSectionView)
    }
}

- (void) setSection:(GSection *)sectionValue
{
    YRelease(section_)
    section_ = [sectionValue retain];
    
    if (!sectionValue)
    {
        return;
    }
    
    [self showDataToView];
    
    [self reAdjustLayout];
}

- (void) setFrame:(CGRect)frame
{
    [super setFrame:frame];
    [self reAdjustLayout];
    
}

- (void) reAdjustLayout
{
    float origin_y__ = 0;
    for (GSubSectionView * subView in self.subSectionViews)
    {
        float height = [subView needHeight];
        [subView setFrame:CGRectMake(0, origin_y__, self.width, height)];
        
        origin_y__ += height;
    }
}


- (float) needHeight
{
    float result = 20;
    for (GSubSectionView * subView in self.subSectionViews)
    {
        result += [subView needHeight];
    }
    return result;
}

@end
